# 比较表格数据


class Spreedsheet:
    def __init__(self, wb):
        self.wb = wb
        self.mod_cells = []
        self.ws = self.wb.active

    def diff(self, other_spreedsheet, start_row, max_col):
        """比较表格某个区域的数据，返回修改的单元格坐标"""
        ws = self.ws
        other_ws = other_spreedsheet.wb.active

        other_ws_mod_cells = []
        for row in ws.iter_rows(min_row=start_row, max_col=max_col):
            for cell in row:
                # 要判断是否已经是修改过的单元格，避免重复修改
                # 修改过的单元格就是别的表已经改过了，不能再改
                if cell.coordinate in self.mod_cells:
                    continue
                elif cell.value != other_ws[cell.coordinate].value:
                    print(
                        "Diff",
                        cell.coordinate,
                        cell.value,
                        other_ws[cell.coordinate].value,
                    )
                    try:
                        cell.value = round(float(other_ws[cell.coordinate].value), 2)

                    except ValueError:
                        cell.value = other_ws[cell.coordinate].value
                    except TypeError:
                        cell.value = other_ws[cell.coordinate].value

                    self.mod_cells.append(cell.coordinate)
                    other_ws_mod_cells.append(cell.coordinate)

        return other_ws_mod_cells
